/*
 * Copyright (c) 2019 The sky Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qinker.robot.hook.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.qinker.robot.hook.BuildConfig;
import com.qinker.robot.hook.Constant;
import com.qinker.robot.hook.R;
import com.qinker.robot.hook.data.VersionManager;
import com.qinker.robot.hook.plugin.interfaces.XVersionManager;
import com.qinker.robot.hook.ui.dialog.DingDingDialog;
import com.qinker.robot.hook.ui.util.DialogUtil;
import com.sky.xposed.common.ui.view.ItemMenu;
import com.sky.xposed.common.util.PackageUtil;
import com.sky.xposed.common.util.ToastUtil;

public class MainActivity extends Activity {

    private ItemMenu imVersion;
    private ItemMenu imDingVersion;
    private TextView tvSupportVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化
        ToastUtil.getInstance().init(getApplicationContext());

        XVersionManager versionManager = new VersionManager
                .Build(getApplicationContext())
                .build();

        imVersion = (ItemMenu) findViewById(R.id.im_version);
        imDingVersion = (ItemMenu) findViewById(R.id.im_ding_version);
        tvSupportVersion = (TextView) findViewById(R.id.tv_support_version);

        imVersion.setDesc("v" + BuildConfig.VERSION_NAME);
        imDingVersion.setDesc(getDingVersionName());

        String builder = ("是否支持当前版本: " + (versionManager.isSupportVersion() ? "是" : "请联系开发更新版本")) +
                "\n支持的版本: " + versionManager.getSupportVersion();
        tvSupportVersion.setText(builder);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (BuildConfig.DEBUG) {
            getMenuInflater().inflate(R.menu.activity_main_menu, menu);
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.menu_settings) {
            // 显示
            DingDingDialog dialog = new DingDingDialog();
            dialog.show(getFragmentManager(), "setting");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.im_about:
                // 关于
                DialogUtil.showAboutDialog(this);
                break;
            default:
                break;
        }
    }

    private String getDingVersionName() {

        // 获取版本名
        PackageUtil.SimplePackageInfo info = PackageUtil
                .getSimplePackageInfo(this, Constant.Rimet.PACKAGE_NAME);

        return info == null ? "Unknown" : "v" + info.getVersionName();
    }
}
